﻿using System;
using System.Collections.Generic;
using DONGACS.DTO;
using DONGACS.SERVICE;

namespace QLPKWeb.BussinessDelegate.PhanQuyen
{
    public class AuthenticationServiceDelegate
    {
        UsersInRolesService UsersInRolesService = new UsersInRolesService();
        PermissionsPerUsersService PermissionsPerUsersService = new PermissionsPerUsersService();
        PathsService PathsService = new PathsService();
        RolesInPathsService RolesInPathsService = new RolesInPathsService();
        PermissionsAllUsersService PermissionsAllUsersService = new PermissionsAllUsersService();
        PermissionsPublicService PermissionsPublicService = new PermissionsPublicService();
        RolesService RolesService = new RolesService();

        public List<string> GetPermissionListPublic() // get danh sách permissions public (các đựoc quyền sử dụng khi chưa login hệ thống )
        {
            var permissionsList = new List<string>();

            IList<PathsDto> pathsDtoResult = new List<PathsDto>();


            ////Get Paths By PathId at PermissionsPublic (trong table TB_PermissionsPublic)
            IList<PermissionsPublicDto> permissionsPublicDtos = PermissionsPublicService.GetAllPermissionsPublic();

            foreach (PermissionsPublicDto permissionsPublicDto in permissionsPublicDtos)
            {
                PathsDto pathsDto = PathsService.GetPathsByPathId(Convert.ToString(permissionsPublicDto.PathId));
                if (!pathsDtoResult.Contains(pathsDto))
                {
                    pathsDtoResult.Add(pathsDto);
                }
            }

            ////Get all Url in pathsDtoResult
            foreach (PathsDto dto in pathsDtoResult)
            {
                //Paths a = new Paths();
                if (!permissionsList.Contains(dto.Url))
                {
                    string permission = string.Empty;
                    string[] controllerAction = dto.Url.Split('/');
                    foreach (string s in controllerAction)
                    {
                        if (!String.IsNullOrEmpty(s))
                        {
                            permission += s + "_";
                        }
                    }

                    if (!String.IsNullOrEmpty(permission))
                    {
                        permission = permission.Substring(0, permission.Length - 1);
                    }

                    permissionsList.Add(permission);
                }
            }

            return permissionsList;
        }

        /// <summary>
        /// Get list Url for a specify user
        /// </summary>
        /// <param name="userId">
        /// The user id.
        /// </param>
        /// <returns>
        /// A list String
        /// </returns>
        public List<string> GetPermissionListForUser(int userId)
        {
            var permissionsList = new List<string>();

            PathsDto pathsDto;
            IList<PathsDto> pathsDtoResult = new List<PathsDto>();

            IList<UsersInRolesDto> usersInRoleses = UsersInRolesService.GetUsersInRolesByUserId(Convert.ToString(userId));

            foreach (UsersInRolesDto usersInRolesDto in usersInRoleses)
            {
                if(RolesService.GetRolesByID(Convert.ToString(usersInRolesDto.RoleId)).IsEnable) // nếu role đang ở trạng thái kích hoặt(được sử dụng trong hệ thống)
                {
                    ////Find RoleInPath for each Role by RoleId
                    IList<RolesInPathsDto> rolesInPathsDtos = RolesInPathsService.GetRolesInPathsByRolesId(Convert.ToString(usersInRolesDto.RoleId));

                    ////Get paths by PathId In RolesInPath 
                    foreach (RolesInPathsDto rolesInPathsDto in rolesInPathsDtos)
                    {
                        pathsDto = PathsService.GetPathsByPathId(Convert.ToString(rolesInPathsDto.PathId));
                        if (pathsDto != null && !pathsDtoResult.Contains(pathsDto))
                        {
                            pathsDtoResult.Add(pathsDto);
                        }
                    }
                }
            }

            ////Get Paths By PathId at PermissionsAllUsers
            IList<PermissionsAllUsersDto> permissionsAllUsersDtos = PermissionsAllUsersService.GetAllPermissionsAllUsers();

            foreach (PermissionsAllUsersDto permissionsAllUsersDto in permissionsAllUsersDtos)
            {
                //pathsDto = this.PathsService.GetPathsByPathId(permissionsAllUsersDto.ID.ToString());
                pathsDto = PathsService.GetPathsByPathId(Convert.ToString(permissionsAllUsersDto.PathId));
                if (pathsDto != null && !pathsDtoResult.Contains(pathsDto))
                {
                    pathsDtoResult.Add(pathsDto);
                }
            }
            ////Get Paths By PathId at PermissionsPerUsers

            IList<PermissionsPerUsersDto> permissionsPerUserDtos = PermissionsPerUsersService.GetPermissionsPerUserByUserId(Convert.ToString(userId));

            foreach (PermissionsPerUsersDto permissionsPerUserDto in permissionsPerUserDtos)
            {
                pathsDto = PathsService.GetPathsByPathId(Convert.ToString(permissionsPerUserDto.PathId));
                if (pathsDto != null && !pathsDtoResult.Contains(pathsDto))
                {
                    pathsDtoResult.Add(pathsDto);
                }
            }

            ////Get all Url in pathsDtoResult
            foreach (PathsDto dto in pathsDtoResult)
            {
                //Paths a = new Paths();
                if (!permissionsList.Contains(dto.Url))
                {
                    string permission = string.Empty;
                    string[] controllerAction = dto.Url.Split('/');
                    foreach (string s in controllerAction)
                    {
                        if (!String.IsNullOrEmpty(s))
                        {
                            permission += s + "_";
                        }
                    }

                    if (!String.IsNullOrEmpty(permission))
                    {
                        permission = permission.Substring(0, permission.Length - 1);
                    }

                    permissionsList.Add(permission);
                }
            }

            return permissionsList;
        }

        //public void InitSessionDetail(UsersDto dto)
        //{
        //    SessionDetail.SetUSERID(dto.ID);
        //    SessionDetail.SetUSERNAME(dto.UserName);
        //    SessionDetail.SetPASSWORD(dto.Password);
        //    SessionDetail.SetLASTNAME(dto.LastName);
        //    SessionDetail.SetFIRSTNAME(dto.FirstName);
        //    SessionDetail.SetDEPARTMENT(dto.Department);
        //    SessionDetail.SetJOBTITLE(dto.JobTitle);
        //    SessionDetail.SetISADMIN(dto.IsAdministrator);
        //    SessionDetail.SetEMAIL(dto.Email);
        //    SessionDetail.SetLAST_LOGIN(dto.LastLoginDate);
        //    //SessionDetail.PermissionList = null;
        //    SessionDetail.SetPermissionList(GetPermissionListForUser(dto.ID));
        //}
        
        
    }
}
